﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;

namespace _4DJackPot
{
    public partial class MainForm : Form
    {
        public bool isLogin { get; set; }

        public DockPanel dockPanel;
        public DockPanel GetDockPanel()
        {
            return this.dockPanel;
        }

        public MainForm()
        {
            InitializeComponent();

            isLogin = true;
        }

        
        private void OpenDockPanel(Dock Dock)
        {
            // 이미 열려 있는 탭의 경우 해당 탭이 활성화 되도록 수정 05.29 민준
            for (int i = 0; i < dockPanel.Contents.Count; i++)
            {
                if (dockPanel.Contents[i].DockHandler.Form.Name == Dock.Name)
                {
                    dockPanel.Contents[i].DockHandler.Form.Activate();
                    return;
                }
            }

            Dock.Show(dockPanel);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Visible = false;

            Login login = new Login();

            if (DialogResult.OK == login.ShowDialog(this)) // 메인폼(Owner) - 로그인폼(Owned) 관계 형성

                this.Visible = true; // 로그인 성공시 Main폼을 보여준다.
            else
                this.Close(); // 로그인 실패시(DialogResult 값이 OK가 아닐 경우)


            InitWindow();
        }

        private void InitWindow()
        {
            try
            {
                
                dockPanel = new DockPanel();
                dockPanel.SuspendLayout(true);
                dockPanel.TabStop = false;

                /* Dock Panel Skin */
                AutoHideStripSkin autoHideSkin = new AutoHideStripSkin();
                // autoHideSkin.DockStripGradient.StartColor = Color.AliceBlue;
                // autoHideSkin.DockStripGradient.EndColor = Color.Blue;
                autoHideSkin.DockStripGradient.LinearGradientMode = System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal;
                autoHideSkin.TabGradient.StartColor = SystemColors.Control;
                autoHideSkin.TabGradient.EndColor = SystemColors.ControlDark;
                autoHideSkin.TabGradient.TextColor = SystemColors.ControlText;
                autoHideSkin.TextFont = new Font("굴림", 9);

                dockPanel.Skin.AutoHideStripSkin = autoHideSkin;

                DockPaneStripSkin dockPaneSkin = new DockPaneStripSkin();
                dockPaneSkin.DocumentGradient.DockStripGradient.StartColor = Color.RoyalBlue;
                dockPaneSkin.DocumentGradient.DockStripGradient.EndColor = Color.Lavender;

                dockPaneSkin.DocumentGradient.ActiveTabGradient.StartColor = Color.RoyalBlue;
                dockPaneSkin.DocumentGradient.ActiveTabGradient.EndColor = Color.LightBlue;
                dockPaneSkin.DocumentGradient.ActiveTabGradient.TextColor = Color.White;

                dockPaneSkin.DocumentGradient.InactiveTabGradient.StartColor = Color.DarkGray;
                dockPaneSkin.DocumentGradient.InactiveTabGradient.EndColor = Color.White;
                dockPaneSkin.DocumentGradient.InactiveTabGradient.TextColor = Color.Black;

                dockPaneSkin.TextFont = new Font("굴림", 9);

                dockPanel.Skin.DockPaneStripSkin = dockPaneSkin;
                /* Dock Panel Skin End */

                dockPanel.Dock = DockStyle.Fill;
                dockPanel.BackColor = Color.Beige;
                Controls.Add(dockPanel);
                dockPanel.BringToFront();

                while (dockPanel.Contents.Count() > 0)
                {
                    dockPanel.Contents[0].DockHandler.Form.Close();
                }

               
                dockPanel.ResumeLayout();
                if(Global.permissionString == "1") // 일반사용자
                {
                    tSB_Buy.Visible = true;
                    tSB_Results.Visible = true;
                    tSB_Analytics.Visible = true;
                    tSB_MyInfo_Results.Visible = true;
                    tSB_MyInfo_ChangePW.Visible = true;


                    tSB_CompanyGamesList.Visible = false;
                    tSB_CompanyChangeMyPassword.Visible = false;

                }
                else
                {
                    tSB_Buy.Visible = false;
                    tSB_Results.Visible = false;
                    tSB_Analytics.Visible = false;
                    tSB_MyInfo_Results.Visible = false;
                    tSB_MyInfo_ChangePW.Visible = false;


                    tSB_CompanyGamesList.Visible = true;
                    tSB_CompanyChangeMyPassword.Visible = true;
                }

                OpenDockPanel(new Home());
            }
            catch (Exception e1)
            {
                
            }
        }

        private void tSB_Buy_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new BUY());
        }

        private void tSB_Analytics_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new Analytics());

        }

        private void tSB_Results_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new Results());
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (isLogin == true) { 
                    if (DialogResult.Yes ==
                            MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        if (Global.dBAdapter != null) Global.dBAdapter.Dispose();
                        if (Global.dBTrans != null) Global.dBTrans.Dispose();
                        if (Global.dBCmd != null) Global.dBCmd.Dispose();
                        if (Global.dBCon != null) Global.dBCon.Close();

                        Application.ExitThread();
                        Environment.Exit(0);
                    }
                    else e.Cancel = true;
                }
            }
            catch (Exception e1)
            {
                FileLog sPCFileLog = new FileLog();
                sPCFileLog.WriteErrorLog(e1.StackTrace + "\n" + e1.Message);
            }
        }

        private void tSB_Home_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new Home());
        }

        private void tSB_MyInfo_Results_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new MyResults());
        }

        private void tSB_MyInfo_ChangePW_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new ChangePassword());
        }

        private void tSB_GamesList_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new GamesList());
        }

        private void tSB_GamesInformation_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new GameInformation());
        }

        private void tSB_GameSplit_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new GameSplit());
        }

        private void tSB_CompanyGamesList_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new CompanyGamesList());
        }

        private void tSB_CompanyChangeMyPassword_Click(object sender, EventArgs e)
        {
            OpenDockPanel(new CompanyChangeMyPassword());
        }

        // 로그아웃 시 실행될 코드들
        private void tSB_Logout_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Do you want to logout?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Global.idString = "";
                Global.pwString = "";
                Global.nameString = "";
                Global.permissionString = "";
                isLogin = false;

                this.Visible = false;

                Login login = new Login();

                if (DialogResult.OK == login.ShowDialog(this)) // 메인폼(Owner) - 로그인폼(Owned) 관계 형성

                    this.Visible = true; // 로그인 성공시 Main폼을 보여준다.
                else
                    this.Close(); // 로그인 실패시(DialogResult 값이 OK가 아닐 경우)


                InitWindow();

            }
        }


        //Av6Yb4DD5ys8 

    }
}
